define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'config/value/index' + location.search,
                    add_url: 'config/value/add/cid/'+ Fast.api.query('cid'),
                    edit_url: 'config/value/edit',
                    del_url: 'config/value/del',
                    multi_url: 'config/value/multi',
                    import_url: 'config/value/import',
                    table: 'config_value',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'option_id',
                sortOrder: 'asc',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'option_id', title: __('Option_id')},
                        {field: 'option_name', title: __('Option_name'), operate: 'LIKE'},
                        {field: 'cid', title: __('Cid'),visible:false,switchable:false},
                        {field: 'group_name', title: __('Cid'),operate: false},
                        {field: 'parent.option_name', title: __('Pid'),operate: false,visible: false,switchable: false},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ],
                onLoadSuccess: function (data) {
                    if (data.total && data.rows[0]['pid']) {
                        table.bootstrapTable('showColumn','parent.option_name');
                    }else {
                        table.bootstrapTable('hideColumn','parent.option_name');
                    }
                }
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {

            let cid = Fast.api.query('cid');
            if (cid === '3') {
                $('#c-pid').data('params',function () {
                    return {custom:{cid:2,pid:0}};
                });
                $('.pidRow').removeClass('hidden');
            }else if(cid === '4') {
                $('.iconRow').removeClass('hidden');
            }


            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
